<template>
	<view>
		<view class="address-section">
			<view class="address-content">
				<uni-icons type="location" size="30"></uni-icons>
				<view class="cen">
					<view class="top">
						<text class="name">{{order.deliveryName}}</text>
						<text class="mobile">{{order.deliveryContactNo}}</text>
					</view>
					<text class="address">{{order.deliveryProvince}}{{order.deliveryCity}}{{order.deliveryArea}}{{order.deliveryStreet}}</text>
				</view>
			</view>
		</view>
		<view class="time-section">
			<view class="time-content">
				<uni-icons type="calendar" size="30"></uni-icons>
				<view class="cen">
					<view class="top">
						<text class="name">预约时间</text>
					</view>
					<text class="time">{{order.appointmentStartTime}} - {{order.appointmentEndTime}}</text>
				</view>
			</view>
		</view>
		<view class="merchant-section" v-if="order.merchantDTO">
			<view class="merchant-content">
				<uni-icons type="person" size="30"></uni-icons>
				<view class="cen">
					<view class="top">
						<text class="title">服务人员</text>
					</view>
					<text class="name">{{order.merchantDTO.merchantName}}{{order.merchantDTO.mobileNo}}</text>
					<!-- <view class="top">
						<text class="">{{order.merchantDTO.merchantName}}</text>
						<text class="mobile">{{order.merchantDTO.mobileNo}}</text>
					</view> -->
				</view>
			</view>
		</view>
		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b" v-for="orderProduct in order.orderProductDTOList">
				<text class="cell-tip">{{orderProduct.productDTO.productName}}</text><text style="margin-left:30px">X {{orderProduct.productUnit}}</text>
			</view>
		</view>
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">订单编号</text>
				<text class="cell-tip">￥{{order.orderNo}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">订单金额</text>
				<text class="cell-tip red">￥{{order.orderAmount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<view class="cell-icon">
					券
				</view>
				<text class="cell-tit clamp">优惠券</text>
				<text class="cell-tip" v-if="coupons.length==0">
					<text>无可用优惠券</text>
				</text>
				<text class="cell-tip active" v-if="coupons.length>0" @click="showCoupon">
					<text v-if="!selectedCoupon.userCouponUuid">{{coupons.length}}张优惠券可用</text>
					<text class="coupon-name" v-if="selectedCoupon.userCouponUuid">
						{{selectedCoupon.couponDTO.name}}
					</text>
					<text class="deduct-amount" v-if="selectedCoupon.userCouponUuid">
						{{deductAmount}}
					</text>
				</text>
				<uni-icons class="close" type="clear" color="#909399" size="20" @click="clearSelectedCoupon"
					v-if="selectedCoupon.userCouponUuid"></uni-icons>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">共优惠</text>
				<text class="cell-tip">-￥{{deductAmount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">已支付定金</text>
				<text class="cell-tip">-￥{{order.prepaidAmount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">实付金额</text>
				<text class="cell-tip red">￥{{paymentAmount}}</text>
			</view>
		</view>
		
		<view class="action-section">
			<view class="button-base" @click="pay">立即支付</view>
		</view>
		
		<!-- 优惠券面板 -->
		<uni-popup ref="showcoupon" type="bottom">
			<scroll-view scroll-y="true" class="coupon-list" @touchmove.stop>
				<user-coupon-item :coupon="item" action-text="立即使用" @eventClick="selectCoupon(item)"
					v-for="item in coupons"></user-coupon-item>
			</scroll-view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import uniPopup from "@/components/uni-popup/uni-popup.vue"
	import userCouponItem from "@/components/user-coupon-item.vue"
	export default {
		data() {
			return {
				orderNo: '',
				order: {},
				coupons: [], //有效优惠券
				selectedCoupon: {}, //选择使用的优惠券
				deductAmount:0.00,
				paymentAmount:0.00
			}
		},
		components: {
			uniPopup,
			userCouponItem
		},
		onLoad(option) {
			//订单数据
			this.orderNo = option.orderNo;
			this.inquiryOrder(this.orderNo);
			this.searchCoupon();
		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		methods: {
			//查询订单
			inquiryOrder(orderNo) {
				this.$api.request.orderDetail({
					orderNo: orderNo
				}, res => {
					if (res.body.status.statusCode === '0') {
						this.order = res.body.data;
						this.paymentAmount = this.getOrderPaymentAmount(this.order.orderAmount, this.order.prepaidAmount,this.deductAmount);
					} else {
						console.log(res.body.status.errorDesc);
					}
				});
			},
			//搜索可用优惠券
			searchCoupon() {
				var keyArray = ['USER', 'IS_EXPIRED', 'IS_USED', 'IS_STARTED'];
				let postData = {
					keyArray: keyArray,
					userUuid: this.userInfo.userUuid,
					expired: false,
					used: false,
					started: true,
					startIndex: 0,
					pageSize: 50 //默认查询50张有效优惠券, 用户大于50张则多余部分不会显示
				};
				//加载中
				this.$api.request.userCoupon(postData, res => {
					if (res.body.status.statusCode === '0') {
						var coupons = res.body.data.userCoupons;
						this.coupons = coupons;
					}
				}, true);
			},
			showCoupon(n) {
				this.$nextTick(() => {
					this.$refs['showcoupon'].open()
				})
			},
			// 不使用优惠券
			clearSelectedCoupon() {
				this.selectedCoupon = {};
				this.deductAmount = 0.00;
				this.paymentAmount = this.getOrderPaymentAmount(this.order.orderAmount, this.order.prepaidAmount,this.deductAmount);
			},
			// 选中优惠券
			selectCoupon(item) {
				//计算当前商品使用优惠券后的优惠金额
				let postData = {
					orderNo: this.orderNo,
					userCouponDTO: {
						couponDTO: {
							couponUuid: item.couponDTO.couponUuid
						}
					}
				}
				this.$api.request.calculateOrderCouponAmount(postData, res => {
					if (res.body.status.statusCode === '0') {
						this.deductAmount = res.body.data.deductAmount;
						this.paymentAmount = this.getOrderPaymentAmount(this.order.orderAmount, this.order.prepaidAmount,this.deductAmount);
						this.selectedCoupon = item;
						this.$refs.showcoupon.close();
					} else {
						this.$api.msg(res.body.status.errorDesc);
					}
				}, false);
			},
			//支付尾款
			pay() {
				//先更新订单使用的优惠券
				if(this.selectedCoupon.userCouponUuid){
					let postData = {
						orderNo: this.orderNo,
						userCouponDTO: {
							userCouponUuid: this.selectedCoupon.userCouponUuid
						}
					}
					this.$api.request.updateOrderCoupon(postData, res => {
						if (res.body.status.statusCode === '0') {
							uni.navigateTo({
								url: '/pages/money/pay?orderNo=' + this.order.orderNo+'&paymentAmount='+(this.paymentAmount)
							})
						} else {
							this.$api.msg(res.body.status.errorDesc);
						}
					}, false);
				}else{
					uni.navigateTo({
						url: '/pages/money/pay?orderNo=' + this.order.orderNo+'&paymentAmount='+(this.paymentAmount)
					})
				}
				
			},
			getOrderPaymentAmount(orderAmount, prepaidAmount, deductAmount){
				return (this.order.orderAmount - this.order.prepaidAmount - this.deductAmount).toFixed(2);
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100upx;
	}
	
	.order-status{
		background: #fff;
		padding: 5px 15px 5px 20px;
		color: #333;
		font-size: 16px;
		font-weight: bold;
	}
	
	.time-section {
		background: #fff;
		position: relative;
	
		.time-content {
			display: flex;
			align-items: center;
			padding: 20px;
		}
	
		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
			padding: 0 10px;
		}
	
		.name {
			font-size: 34upx;
			margin-right: 24upx;
		}
	
		.mobile {
			font-size: 34upx;
		}
	
		.time {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}
	}
	
	.merchant-section {
		background: #fff;
		position: relative;
	
		.merchant-content {
			display: flex;
			align-items: center;
			padding: 20px;
		}
	
		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
			padding: 0 10px;
		}
	
		.title {
			font-size: 34upx;
			margin-right: 24upx;
		}
		.name {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}

	}
	
	.address-section {
		background: #fff;
		position: relative;
	
		.address-content {
			display: flex;
			align-items: center;
			padding: 20px;
		}
	
		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
			padding: 0 10px;
		}
	
		.name {
			font-size: 34upx;
			margin-right: 24upx;
		}
	
		.mobile {
			font-size: 34upx;
		}
	
		.address {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}
	}

	.goods-section {
		margin-top: 16upx;
		background: #fff;
		padding-bottom: 1px;

		.g-header {
			display: flex;
			align-items: center;
			height: 84upx;
			padding: 0 30upx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50upx;
			height: 50upx;
			border-radius: 100px;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			display: flex;
			margin: 20upx 30upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 4upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26upx;
				color: $font-color-light;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32upx;
				color: $font-color-dark;
				padding-top: 10upx;

				.price {
					margin-bottom: 4upx;
				}

				.number {
					font-size: 26upx;
					color: $font-color-base;
					margin-left: 20upx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		margin-top: 16upx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: #f85e52;
			border-radius: 4upx;
			margin-right: 12upx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 26upx;
			color: $font-color-dark;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}

			&.red {
				color: $base-color;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}
	.action-section{
		margin-top:30px;
	}
</style>
